package com.etrans.web.gps.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
 


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
 
 
public class ParseXml {
	public Map<String,Object>map = new HashMap<String,Object>();
	
	public  Map<String,Object> parse(String soap) throws DocumentException{
		Document doc = DocumentHelper.parseText(soap);//报文转成doc对象
		Element root = doc.getRootElement();//获取根元素，准备递归解析这个XML树
		getCode(root);
		return map;
	}
	
	public  void getCode(Element root){
		if(root.elements()!=null){
			List<Element>list = root.elements();//如果当前跟节点有子节点，找到子节点
			for(Element e:list){//遍历每个节点
				if(e.elements().size()>0){
					getCode(e);//当前节点不为空的话，递归遍历子节点；
				}
				if(e.elements().size()==0){
					map.put(e.getName(), e.getTextTrim());
				}//如果为叶子节点，那么直接把名字和值放入map
			}
		}
	}
	
	public static void main(String[]args) throws DocumentException{
		String xml="<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><GetCarInfoResponse xmlns='http://tempuri.org/'><GetCarInfoResult><xs:schema id='GetCarInfo' xmlns='' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'><xs:element name='GetCarInfo' msdata:IsDataSet='true' msdata:MainDataTable='Vehicle' msdata:UseCurrentLocale='true'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='Vehicle'><xs:complexType><xs:sequence><xs:element name='OperatingID' type='xs:string' minOccurs='0' /><xs:element name='OwnerUnit' type='xs:string' minOccurs='0' /><xs:element name='CarNumber' type='xs:string' minOccurs='0' /><xs:element name='FactoryID' type='xs:string' minOccurs='0' /><xs:element name='OperatingType' type='xs:string' minOccurs='0' /><xs:element name='CarType' type='xs:string' minOccurs='0' /><xs:element name='NumberColor' type='xs:string' minOccurs='0' /><xs:element name='Capacity' type='xs:string' minOccurs='0' /><xs:element name='VclLenght' type='xs:string' minOccurs='0' /><xs:element name='VclWidth' type='xs:string' minOccurs='0' /><xs:element name='VclHeight' type='xs:string' minOccurs='0' /><xs:element name='VclState' type='xs:string' minOccurs='0' /><xs:element name='VclTecLevel' type='xs:string' minOccurs='0' /><xs:element name='CurCheckDate' type='xs:string' minOccurs='0' /><xs:element name='NextCheckDate' type='xs:string' minOccurs='0' /><xs:element name='EngineNumber' type='xs:string' minOccurs='0' /><xs:element name='VIN' type='xs:string' minOccurs='0' /><xs:element name='REMARK' type='xs:string' minOccurs='0' /><xs:element name='PeccancyState' type='xs:string' minOccurs='0' /><xs:element name='LicenseNumber' type='xs:string' minOccurs='0' /><xs:element name='LicenseDate' type='xs:string' minOccurs='0' /><xs:element name='OUTMILLDATE' type='xs:string' minOccurs='0' /><xs:element name='FuelType' type='xs:string' minOccurs='0' /><xs:element name='NextAssessDate' type='xs:string' minOccurs='0' /><xs:element name='MType' type='xs:string' minOccurs='0' /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'><GetCarInfo xmlns=''><Vehicle diffgr:id='Vehicle1' msdata:rowOrder='0' diffgr:hasChanges='inserted'><OperatingID>450100030183</OperatingID><OwnerUnit>南宁大明山运输有限责任公司</OwnerUnit><CarNumber>桂AN8113</CarNumber><FactoryID>柯斯达牌SCT6705GRB53LEXY</FactoryID><OperatingType>县内包车客运,县际包车客运</OperatingType><CarType>大型普通客车</CarType><NumberColor>黄色</NumberColor><Capacity>20座</Capacity><VclLenght>7005</VclLenght><VclWidth>2040</VclWidth><VclHeight>2772</VclHeight><VclState>营运</VclState><VclTecLevel>一级</VclTecLevel><CurCheckDate>2018-03-28</CurCheckDate><NextCheckDate>2019-03-19</NextCheckDate><EngineNumber>H186633</EngineNumber><VIN>LFME55813JS011367</VIN><REMARK>GPS0</REMARK><PeccancyState>正常</PeccancyState><LicenseNumber>450103101651</LicenseNumber><LicenseDate>2018-03-28</LicenseDate><OUTMILLDATE>2018-01-03</OUTMILLDATE><FuelType>汽油</FuelType><NextAssessDate>2018-09-26</NextAssessDate><MType>柯斯达牌SCT6705GRB53LEXY</MType></Vehicle></GetCarInfo></diffgr:diffgram></GetCarInfoResult><ErrMsg /></GetCarInfoResponse></soap:Body></soap:Envelope>";
		//初始化报文，调用parse方法，获得结果map，然后按照需求取得字段，或者转化为其他格式
		Map<String,Object> map = new ParseXml().parse(xml);
		//获得字段s:SourceSysId的值;
		String id = map.get("SourceSysId").toString();
		System.out.println("id=="+id);
	}
}

